package net.chenlin.dp.modules.newBusiness.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.modules.newBusiness.dao.DeviceMessageMapper;
import net.chenlin.dp.modules.newBusiness.entity.dto.DeviceMessageDto;
import net.chenlin.dp.modules.newBusiness.entity.po.DeviceMessagePo;
import net.chenlin.dp.modules.newBusiness.entity.vo.DeviceQueryReplayVo;
import net.chenlin.dp.modules.newBusiness.service.DeviceMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Yansgm
 * @Description:
 * @Date: 2025-04-14 20:45:57
 */
@Service
@Slf4j
public class DeviceMessageServiceImpl implements DeviceMessageService {

    @Autowired
    private DeviceMessageMapper mapper;

    public static void main(String[] args) {
        String msg = "{\n" +
                "\"monitor_id\":\"84597878\",\n" +
                "\"timestamp\":\"2024-10-19T14:52:00Z\",\n" +
                "\"meteringstart\":\"2024-10-19T14:52:00Z\",\n" +
                "\"meteringend\":\"2024-10-19T14:52:00Z\",\n" +
                "\"meteringvalue\":{\n" +
                "\"meteringA\":29.123,\n" +
                "\"meteringB\":29.123,\n" +
                "\"meteringC\":29.123,\n" +
                "},\n" +
                "\"currentvalue\":{\n" +
                "\"currentA\":29.123,\n" +
                "\"currentB\":29.123,\n" +
                "\"currentC\":29.123,\n" +
                "},\n" +
                "\"voltagevalue\":{\n" +
                "\"voltageA\":29.123,\n" +
                "\"voltageB\":29.123,\n" +
                "\"voltageC\":29.123,\n" +
                "}\n" +
                "}";
        JSONObject jsonObject = JSON.parseObject(msg);
        String meteringvalue = jsonObject.get("meteringvalue").toString();
        JSONObject nextjsonObject = JSON.parseObject(meteringvalue);
        DeviceMessageDto dto = JSON.toJavaObject(jsonObject, DeviceMessageDto.class);
        //Meteringvalue dto = JSON.toJavaObject(nextjsonObject, Meteringvalue.class);
        System.out.println(dto);

    }

    @Override
    public R getQueryData(String deviceNo) {
        DeviceQueryReplayVo vo = mapper.getQueryData(deviceNo);
        return CommonUtils.msg(vo);
    }

    @Override
    public R insertDeviceMessage(String msg) {
        JSONObject jsonObject = JSON.parseObject(msg);
        DeviceMessageDto dto = JSON.toJavaObject(jsonObject, DeviceMessageDto.class);
        DeviceMessagePo po = new DeviceMessagePo();
        BeanUtils.copyProperties(dto,po);
        Integer districtId = mapper.getDistrictId(dto.getMonitorId());
        po.setDistrictId(districtId);
        po.setReportTime(dto.getTimestamp());
        po.setMeteringA(dto.getMeteringvalue().getMeteringA());
        po.setMeteringB(dto.getMeteringvalue().getMeteringB());
        po.setMeteringC(dto.getMeteringvalue().getMeteringC());

        po.setCurrentA(dto.getCurrentvalue().getCurrentA());
        po.setCurrentB(dto.getCurrentvalue().getCurrentB());
        po.setCurrentC(dto.getCurrentvalue().getCurrentC());

        po.setVoltageA(dto.getVoltagevalue().getVoltageA());
        po.setVoltageB(dto.getVoltagevalue().getVoltageB());
        po.setVoltageC(dto.getVoltagevalue().getVoltageC());
        mapper.insertDeviceMessage(po);
        return R.ok();
    }
}
